package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.DictDao;
import com.atguigu.entity.Dict;
import com.atguigu.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lhh
 * @ClassName DictServiceImpl
 * @date 2022/9/17 8:46
 */

@Service(interfaceClass = DictService.class)
@Transactional
public class DictServiceImpl extends BaseServiceImpl<Dict> implements DictService {

    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<Dict> getEntityDao() {
        return dictDao;
    }

        /*{ id:'011', name:'n1.n1', isParent:true},*/

    public List<Map<String, Object>> findZnodes(Long parentId) {

        List<Dict> dictList=dictDao.findListByParentId(parentId);

        List<Map<String, Object>> nodeList=new ArrayList<Map<String, Object>>();
        for (Dict dict : dictList) {
            Map<String, Object> node=new HashMap();
            node.put("id",dict.getId());
            node.put("name",dict.getName());
            Integer count=dictDao.countParent(dict.getId());
            node.put("isParent",count>0? true: false);

            nodeList.add(node);
        }
        return nodeList;
    }

    public List<Dict> findListByDictCode(String dictCode) {

        Dict dict=dictDao.getByDictCode(dictCode);
        return dictDao.findListByParentId(dict.getId());
    }

    public List<Dict> findListByParentId(Long parentId) {

        List<Dict> dictList=dictDao.findListByParentId(parentId);
        return dictList;
    }

    public String getNameById(Long id) {
        return dictDao.getNameById(id);
    }

}
